package com.mathassistant.client.view;

import java.util.List;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.HasClickHandlers;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.HTMLTable;
import com.google.gwt.user.client.ui.Hyperlink;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.Widget;
import com.mathassistant.client.helper.ClickPoint;
import com.mathassistant.client.presenter.TopicListPresenter.Display;
import com.mathassistant.client.resource.ManagementResources;

public class TopicListView extends Composite implements Display {

	private static TopicListViewUiBinder uiBinder = GWT
			.create(TopicListViewUiBinder.class);

	
	@UiField Hyperlink addButton;
	@UiField FlexTable flexTable;

	boolean isAdminViewing = false;

	interface TopicListViewUiBinder extends UiBinder<Widget, TopicListView> {
	}

	public TopicListView() {
		initWidget(uiBinder.createAndBindUi(this));

	}

	private void displayTopics(List<String> topicNames) {
		// TODO Auto-generated method stub
		int i = 0;
		flexTable.clear();

		if (topicNames == null || topicNames.size() == 0) {
			// no topic, can make loading here
			return;
		}

		for (final String topicName : topicNames) {
			// dau mui ten
			Image imgMuiTen = new Image(ManagementResources.RESOURCE.muiTen());

			// topic name
			Hyperlink hplink = new Hyperlink();
			hplink.setHTML(topicName);
			hplink.addStyleName("pointer");

			flexTable.setWidget(i, 0, imgMuiTen);
			flexTable.setWidget(i, 1, hplink);
			flexTable.getCellFormatter().addStyleName(i, 1, "itemTopic");
			
			// property image
			final Image image = new Image(
					ManagementResources.RESOURCE.buttonDetails());
			image.addStyleName("pointer");

			flexTable.setWidget(i, 2, image);

			image.setVisible(isAdminViewing);

			i++;
		}
	}
	
	@Override
	public HasClickHandlers getHyperlinkAddTopic() {
		// TODO Auto-generated method stub
		return addButton;
	}

	@Override
	public HasClickHandlers getListTopics() {
		// TODO Auto-generated method stub
		return flexTable;
	}

	@Override
	public void setData(List<String> topics) {
		// TODO Auto-generated method stub
		displayTopics(topics);
	}
	
	@Override
	public void resetData() {
		// TODO Auto-generated method stub
		flexTable.clear();
	}
	
	@Override
	public int getClickedRow(ClickEvent event) {
		// TODO Auto-generated method stub
		int selectedRow = -1;
		HTMLTable.Cell cell = this.flexTable.getCellForEvent(event);

		if (cell != null) {
			// Suppress clicks if not on the property button
			selectedRow = cell.getRowIndex();
		}

		return selectedRow;
	}

	@Override
	public ClickPoint getClickedPoint(ClickEvent event) {
		// TODO Auto-generated method stub
		final Image img;
		int selectedRow = -1;
		ClickPoint point = null;
		HTMLTable.Cell cell = this.flexTable.getCellForEvent(event);

		if (cell != null) {
			// Suppress clicks if not on the property button
			if (cell.getCellIndex() > 0) {
				selectedRow = cell.getRowIndex();
				img = (Image) flexTable.getWidget(selectedRow, 2);
				int left = img.getAbsoluteLeft();
				int top = img.getAbsoluteTop();
				point = new ClickPoint(top, left);
			}
		}

		return point;
	}

	@Override
	public Widget asWidget() {
		// TODO Auto-generated method stub
		return this;
	}

	@Override
	public int getClickedPropertyButton(ClickEvent event) {
		// TODO Auto-generated method stub
		int selectedRow = -1;
		HTMLTable.Cell cell = this.flexTable.getCellForEvent(event);

		if (cell != null) {
			// Suppress clicks if not on the property button
			if (cell.getCellIndex() > 1) {
				selectedRow = cell.getRowIndex();
			}
		}

		return selectedRow;
	}

	@Override
	public void setToAdminView() {
		// TODO Auto-generated method stub
		this.addButton.setVisible(true);
		isAdminViewing = true;
	}

	@Override
	public void setToUserView() {
		// TODO Auto-generated method stub
		this.addButton.setVisible(false);
		isAdminViewing = false;
	}

}
